package com.htc.model.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.htc.constants.Constants;
import com.htc.model.dto.FitnessGroupDTO;

public class FitnessGroupModel {
	public static FitnessGroupModel instance;

	public static FitnessGroupModel getInstance() {
		if (instance == null)
			instance = new FitnessGroupModel();
		return instance;
	}

	public boolean checkDB(Context context, String id) {
		boolean flag = false;
		String sql = "SELECT fitness_group_id FROM "
				+ Constants.TBL_FITNESS_GROUP + " where fitness_group_id='"
				+ id + "'";
		DBHelper db = DBHelper.getInstance(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			flag = true;
			break;
		}
		c.close();
		db.close();
		return flag;
	}

	public boolean insertFitGroup(FitnessGroupDTO fitg, Context context) {
		boolean flag = false;
		if (!checkDB(context, fitg.getFitnessGroupId())) {
			String DATABASE_TABLE = Constants.TBL_FITNESS_GROUP;
			ContentValues values = new ContentValues();
			values.put("fitness_group_id", fitg.getFitnessGroupId());
			values.put("fitness_group_name", fitg.getFitnessGroupName());
			values.put("fitness_group_name_ar", fitg.getFitnessGroupNameAr());
			values.put("added_date", fitg.getFitnessGroupAddedDate());
			DBHelper db = DBHelper.getInstance(context);
			flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
			db.close();
		}

		return flag;
	}

	public boolean updateFitGroup(FitnessGroupDTO fitg, Context context) {
		boolean flag = false;

		String DATABASE_TABLE = Constants.TBL_FITNESS_GROUP;
		String where = "fitness_group_id='" + fitg.getFitnessGroupId() + "'";
		ContentValues values = new ContentValues();

		values.put("fitness_group_name", fitg.getFitnessGroupName());
		values.put("fitness_group_name_ar", fitg.getFitnessGroupNameAr());
		values.put("added_date", fitg.getFitnessGroupAddedDate());
		DBHelper db = DBHelper.getInstance(context);
		flag = db.ExcuteUpdate(null, DATABASE_TABLE, values, where, null);
		db.close();
		return flag;
	}

	public void deleteFitGroup(FitnessGroupDTO fitg, Context context) {
		String sql = "fitness_group_id='" + fitg.getFitnessGroupId() + "'";
		DBHelper db = DBHelper.getInstance(context);
		String DATABASE_TABLE = Constants.TBL_FITNESS_GROUP;
		db.deleteTable(DATABASE_TABLE, sql);
		db.close();
	}
}
